﻿namespace AjaxControlToolkit
{
    using System;
    using System.Collections.Specialized;

    public static class CascadingDropDown
    {
        /// <summary>
        /// Helper method to parse the private storage format used to communicate known category/value pairs.
        /// </summary>
        /// <param name="knownCategoryValues">Private storage format string.</param>
        /// <returns>Dictionary of category/value pairs.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Avoiding possible breaking change")]
        public static StringDictionary ParseKnownCategoryValuesString(string knownCategoryValues)
        {
            // Validate parameters
            if (null == knownCategoryValues)
            {
                throw new ArgumentNullException("knownCategoryValues");
            }

            StringDictionary dictionary = new StringDictionary();
            if (null != knownCategoryValues)
            {
                // Split into category/value pairs
                foreach (string knownCategoryValue in knownCategoryValues.Split(';'))
                {
                    // Split into category and value
                    string[] knownCategoryValuePair = knownCategoryValue.Split(':');
                    if (2 == knownCategoryValuePair.Length)
                    {
                        // Add the pair to the dictionary
                        dictionary.Add(knownCategoryValuePair[0].ToLowerInvariant(), knownCategoryValuePair[1]);
                    }
                }
            }

            return dictionary;
        }
    }
}
